package com.hebeu.alipaydemo;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayObject;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePrecreateRequest;
import com.alipay.api.response.AlipayTradePrecreateResponse;
import com.hebeu.config.AlipayConfig;

@WebServlet("/AlipayPrecreate")
public class AlipayPrecreate extends HttpServlet {
	private static final long serialVersionUID = 1L;
  
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.serverUrl, AlipayConfig.appId, AlipayConfig.privateKey, AlipayConfig.format, AlipayConfig.charset, AlipayConfig.alipayPulicKey, AlipayConfig.signType);
		//AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", APP_ID, APP_PRIVATE_KEY, "json", CHARSET, ALIPAY_PUBLIC_KEY, "RSA2"); //获得初始化的AlipayClient
		AlipayTradePrecreateRequest alipayRequest = new AlipayTradePrecreateRequest();//创建API对应的request类
		alipayRequest.setBizContent("{" +
		"    \"out_trade_no\":\"20150320010101005\"," +
		"    \"total_amount\":\"88.88\"," +
		"    \"subject\":\"Iphone6 16G\"," +
		"    \"store_id\":\"NJ_001\"," +
		"    \"timeout_express\":\"90m\"}");//设置业务参数
		 
		AlipayTradePrecreateResponse alipayResponse;
		try {
//			String pd_frpId = request.getParameter("pd_FrpId");
//			System.out.println(pd_frpId);
			alipayResponse = alipayClient.execute(alipayRequest);
			System.out.println(alipayResponse.getBody());
			System.out.println(alipayResponse.getMsg());
			response.sendRedirect("https://cashierem14.alipay.com/standard/lightpay/lightPayCashier.htm?useMcard=false&usePcard=false&usePromotion=false&timeStamp=1526953976876&outBizNo=2018052221001001860259263338&bizIdentity=trade10001&usePoint=false&orderId=052214188842ba96c72401ierc025863&country=CN");

		} catch (AlipayApiException e) {
			e.printStackTrace();
}

	}

}
